package com.sq.partner.manager.dao.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.sq.partner.manager.dao.INlifePlanDao;
import com.sq.partner.manager.mybatis.model.NlifePlan;
import com.sq.partner.manager.mybatis.model.TradAgentInfo;
import com.sq.partner.manager.mybatis.model.TradAgentInfoRela;
import com.sq.partner.manager.util.Page;

@Repository
public class NlifePlanDaoImpl extends BaseDaoImpl implements INlifePlanDao {

	@Override
	public Page<Map<String,Object>> queryPageByCriteria(Page<Map<String,Object>> page)
			throws Exception {
		Map<String,Object> paramMap = page.getMap();
		
		int total = (Integer) this.query("queryNlifePlanCountByFilter", paramMap);
		page.setTotal(total);
		
		List<Map<String,Object>> resultList = this.queryList("queryNlifePlanPageByFilter", paramMap);
		page.setData(resultList);

		return page;
	}
	
	@Override
	public Page<Map<String,Object>> queryNlifeHealthInformPageByCriteria(Page<Map<String,Object>> page)
			throws Exception {
		Map<String,Object> paramMap = page.getMap();
		
		int total = (Integer) this.query("queryNlifeHealthInformCountByFilter", paramMap);
		page.setTotal(total);
		
		List<Map<String,Object>> resultList = this.queryList("queryNlifeHealthInformPageByFilter", paramMap);
		page.setData(resultList);

		return page;
	}

	@Override
	public Page<Map<String,Object>> queryPageByNewCriteria(Page<Map<String,Object>> page)
			throws Exception {
		Map<String,Object> paramMap = page.getMap();
		
		int total = (Integer) this.query("queryUnApproveNlifePlanCountByFilter", paramMap);
		page.setTotal(total);
		
		List<Map<String,Object>> resultList = this.queryList("queryUnApproveNlifePlanPageByFilter", paramMap);
		page.setData(resultList);

		return page;
	}	
	
	@Override
	public NlifePlan queryById(String planNo) throws Exception {
		NlifePlan nlifePlan = (NlifePlan) this.query("queryNlifePlanById", planNo==null?"":planNo);
		return nlifePlan;
	}

	@Override
	public boolean delById(String planNo) throws Exception {
		boolean flag = this.delete("delNlifePlanById", planNo==null?"":planNo);
		return flag;
	}

	@Override
	public String queryNextPlanNo(String planStr) throws Exception {
		Object object = this.query("queryNextPlanNo", planStr);//参数为方案号的前缀
		return object==null?null:object.toString();
	}

	@Override
	public boolean add(NlifePlan nlifePlan) throws Exception {
		nlifePlan.setOperTime(new Date());
		boolean flag = this.add("insertNlifePlan", nlifePlan);
		
		return flag;
	} 

	@Override
	public boolean update(NlifePlan nlifePlan) throws Exception {
		nlifePlan.setOperTime(new Date());
		boolean flag = this.update("updateNlifePlan", nlifePlan);
		
		return flag;
	}

	@Override
	public boolean updateForCheck(NlifePlan nlifePlan) throws Exception {
		nlifePlan.setOperTime(new Date());
		boolean flag = this.update("updateNlifePlanForCheck", nlifePlan);
		
		return flag;
	}
	
	@Override
	public List<NlifePlan> queryPartnerInfoByProdCode(String prodCode) throws Exception {
		return this.queryList("queryisExistPlanOrPartnerInfoByProdCode", prodCode);
	}

	@Override
	public boolean isHasExistNlifePlan(String partnerNo) throws Exception {
		Integer result = (Integer)this.query("queryNlifePlanCountByPartnerNo", partnerNo);
		if(null!=result &&  Integer.valueOf(result)>0){
			return true;
		}
		return false;
	}

	@Override
	public List<Map<String, Object>> impNlifePlan(Map<String, Object> map)
			throws Exception {
		return this.queryList("impNlifePlanByMap", map);
	}

	@Override
	public List<TradAgentInfo> searchTradAgentInfosByplanCode(
			Map<String, Object> maps) throws Exception {
		// TODO Auto-generated method stub
		return this.queryList("searchTradAgentInfosByplanCode", maps);
	}

	@Override
	public List<TradAgentInfoRela> searchTradAgentInfoRelasByplanCode(
			Map<String, Object> maps) throws Exception {
		// TODO Auto-generated method stub
		return this.queryList("searchTradAgentInfoRelasByplanCode", maps);
	}

//	/**
//	 * 查询方案信息列表
//	 * @param name
//	 * @return
//	 * @throws Exception
//	 */
//	@SuppressWarnings("unchecked")
//	@Override
//	public List<NlifePlan> queryPlan(String name, String channelCode) throws Exception{
//		Map<String,Object> paramMap = new HashMap<String,Object>();
//		paramMap.put("name",name);
//		paramMap.put("channelCode", channelCode);
//		return (List<NlifePlan>)this.queryList("queryPlanList", paramMap);
//	}
/**
 * 激活卡方案分页
 */
	@Override
	public List<NlifePlan> queryPageByCriteriaForCard(Map<String, Object> map)
			throws Exception {
		
		return this.queryList("querylifePlanPageByFilterForCard", map);
	}
/**
 * 激活卡方案总条数
 */
	@Override
	public Integer queryCountByCriteriaForCard(Map<String, Object> map)
			throws Exception {
		
		return (Integer) this.query("queryNlifePlanCountByFilterForCard", map);
	}
	

@Override
public boolean isHealthInformExist(String coreProdCode) throws Exception {
	int count = (Integer) this.query("isHealthInformExist", coreProdCode);
	if(count == 0){
		return false;
	}else{
		return true;
	}
}

@Override
public void insertNewHealthInform(Map<String, Object> map) throws Exception {

	
	this.add("insertNewHealthInformByMap", map);
	
}

@Override
public void delNlifeHealthInform(String id) throws Exception {
	this.delete("delNlifeHealthInform", id);
	
}


	
}
